// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'boyi-5g77xpkp0aa0181c'
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();

  var result = {
    code: 0,
    state: 'success',
    error: 'null'
  }

  // 检查参数是否完整
  if (event.date == undefined || event.name == undefined || event.goWhere == undefined || event.stayTime == undefined || event.otherInfo == undefined) {
    result.code = 1;
    result.state = 'fail';
    result.error = '参数不完整';
    return result;
  }

  await cloud.database().collection('goOutInfo').add({
    data: {
      openId: wxContext.OPENID,
      date: event.date,
      name: event.name,
      goWhere: event.goWhere,
      stayTime: event.stayTime,
      otherInfo: event.otherInfo
    }
  }).then(res => {
    console.log(result);
  })

  return result;
}